﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Threading.Tasks;
using Microsoft.Practices.Unity;
using FluentNHibernate.Cfg;
using FluentNHibernate.Automapping;
using Model;



namespace Data.Impl
{
    public class Initializer : IDataTierInitializer
    {
        [Dependency]
        public SessionManager SessionManager { get; set; }


        public void Init(String connectionString)
        {

            var fluent = Fluently.Configure()
                //.Database(FluentNHibernate.Cfg.Db.MsSqlConfiguration.MsSql2008.ConnectionString(connectionString).Driver<SqlAzureClientDriver>())
                .Database(FluentNHibernate.Cfg.Db.MsSqlConfiguration.MsSql2008.ConnectionString(connectionString))
                .Mappings(m =>
                {

                    m.FluentMappings.AddFromAssembly(this.GetType().Assembly);

                    m.AutoMappings.Add(
                        AutoMap.Assemblies(typeof(User).Assembly)
                        .Override<User>(map =>
                        {
                            map.References(x => x.Image).Not.Nullable();
                        })
                        .Override<ContactUs>(map =>
                        {
                           
                        })
                        .Override<Image>(map =>
                        {

                        })
                        .Override<Article>(map =>
                        {
                            map.HasMany(x => x.ArticleImages).Inverse().Cascade.All();
                        })
                        .Override<ArticleImage>(map =>
                        {
                            map.References(x => x.Article).Not.Nullable();
                            map.References(x => x.Image).Not.Nullable();
                        })
                        .Override<JobOffer>(map =>
                        {

                        })
                    );
                });

            SessionManager.NhConfiguration = fluent.BuildConfiguration();
            SessionManager.SessionFactory = SessionManager.NhConfiguration.BuildSessionFactory();
        }
    }



}
